package Utils;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import android.os.Environment;

public class ARFFFileWriter {
	
	// the external memory card, a new directory and the file that we write to.
	private File sdCard;
	private File dir;
	private File mSampleFile;
	private FileWriter f;
	private BufferedWriter writer;
	//make the class Singleton as we will need only ARFFFileWriter in our application
	private static ARFFFileWriter fileWriter = null;

	protected ARFFFileWriter() {
      // Exists only to defeat instantiation.
	}	
	public static ARFFFileWriter getInstance() {
		if(fileWriter == null) {
			fileWriter = new ARFFFileWriter();
		}
		return fileWriter;
	}

		
	/**creating a new file on the disc. filename is given by the user
	 * 
	 * @param fileName
	 */
	public void createFile(String fileName){
		// Initialize the sd card, create the new directory and the file to be
		// written

		sdCard = Environment.getExternalStorageDirectory();
		dir = new File(sdCard.getAbsolutePath() + "/sensor_data");
		dir.mkdirs();
		mSampleFile = new File(dir, fileName);
		try {
			f = new FileWriter(mSampleFile.getAbsoluteFile(), false);
			writeHeader();
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void writeHeader(){
		
		try {
			writer = new BufferedWriter(f);
			//write header
			writer.write("% Group: Andreea Buga/Omar Fouda" + "\n");
			writer.write("% Date: " + this.getSystemDate() + "\n" + "\n");
			writer.write("@RELATION systemsdevelopment" + "\n" + "\n");
			writer.write("@ATTRIBUTE timestamp      NUMERIC" + "\n");
			writer.write("@ATTRIBUTE accelerationx  NUMERIC" + "\n");
			writer.write("@ATTRIBUTE accelerationy  NUMERIC" + "\n");
			writer.write("@ATTRIBUTE accelerationz  NUMERIC" + "\n" + "\n");
			writer.write("@DATA" + "\n");
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	public String getSystemDate(){
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		Date date = new Date();
		return dateFormat.format(date);
	}
	
	//check if file exists so that the user does not overwrite existing files containing data
	public boolean fileExists(String fileName){
		sdCard = Environment.getExternalStorageDirectory();
		dir = new File(sdCard.getAbsolutePath() + "/sensor_data");
		File f=new File(dir, fileName);
		return f.exists();
	}
	
	public void WriteTofile(float sensorX, float sensorY,
			float sensorZ) {

		try {
			String text = System.currentTimeMillis() + ", " + sensorX + ", " + sensorY + ", " + sensorZ;
			writer.append(text);
			writer.newLine();
			writer.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.println("NO FILE");

		}
	}
	public void finishWriting(){
		try {
			writer.flush();
			writer.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/**
	 * method for checking if an input String is a valid filename
	 * @param fileName
	 * @return
	 */
	public boolean isFileNameValid(String fileName){
		boolean isValid = true;
		List<String> invalidCharacters = Arrays.asList("/", "\\", "?", "%", "*", ".", ":", " ", "|", "\"", "<", ">");
		if (fileName.length() == 0)
			return false;
		for (String invalidCharacter:invalidCharacters)
			if (fileName.contains(invalidCharacter))
				return false;
		return true;
	}
}
